<?php
App::import('Vendor', 'SectionJson');
App::import('Vendor', 'CartaJson');
App::import('Vendor', 'CategoriaJson');
class ApiController extends AppController
{

    public $components = array('RequestHandler');
    public $viewClass = 'Json';

    function getSeccionesDia()
    {
        $this->loadModel("Section");
        $this->loadModel("Carta");
        $this->loadModel("CartaCategoria");
        $this->loadModel("Categoria");
        $this->loadModel("CartaSection");
        $db = $this->Section->find('all', array(
            'conditions' => array('Section.activado' => '1')
        ));

//$db = $this->Section->find('all');

        $i = 0;
        $completo = array();

        foreach ($db as $seccion) {
            $object = new SectionJson();
            $object->nombre = $seccion['Section']['nombre'];
            $object->id = $seccion['Section']['id'];
            $object->fondo = $seccion['Section']['fondo'];


            $object->children = array();
            $dbCartas = $this->CartaSection->findAllByIdSeccion($object->id);

            $j = 0;

            foreach ($dbCartas as $carta) {

                $id = $carta['CartaSection']['id_carta'];
                $carta2 = $this->Carta->findById($id);
                $objectCarta = new CartaJson();
                $objectCarta->nombre = $carta2['Carta']['nombre_app'];
                $objectCarta->idCarta = $carta2['Carta']['id'];
                $objectCarta->icon = $carta2['Carta']['icon'];

                $dbCategorias = $this->CartaCategoria->findAllByIdCarta($objectCarta->idCarta);
                $objectCarta->children = array();
                foreach ($dbCategorias as $categoria) {

                    $id = $categoria['CartaCategoria']['id_categoria'];
                    $cat = $this->Categoria->findById($id);
                    $objectCategoria = new CategoriaJson();
                    $objectCategoria->nombre = $cat['Categoria']['nombre'];
                    $objectCategoria->idCategoria = $cat['Categoria']['id'];
                    $objectCategoria->icon = $cat['Categoria']['icon'];
                    $objectCartaCategoria = $this->CartaCategoria->findByIdCartaAndIdCategoria($objectCarta->idCarta, $objectCategoria->idCategoria);
                    $idCartaCategoria = $objectCartaCategoria['CartaCategoria']['id'];
                    $objectCategoria->idCartaCategoria = $idCartaCategoria;
                    $objectCategoria->descripcion = $cat['Categoria']['descripcion'];
                    array_push($objectCarta->children, $objectCategoria);
                }

                array_push($object->children, $objectCarta);
                $j = $j + 1;
            }

//debug($object);

            $completo[$i] = $object;
//debug($seccion->getVirtualField('children'));
//debug($completo[$i]);
            $i = $i + 1;
        }
        $this->set('children', $completo);
        $this->set('_serialize', array('children'));

    }


    function getPlatosByCartaCategoria($id = null)
    {

        $id = $this->request->query['catId'];
        $this->loadModel('CartaCategoriasProducto');
        $this->loadModel('Producto');
        $db = $this->CartaCategoriasProducto->findAllByIdCartaCategoria($id);
//debug($db);
        $ids = array();
        $j = 0;
        foreach ($db as $cartacatpro) {
            $ids[$j] = $cartacatpro['CartaCategoriasProducto']['id_producto'];
            $j = $j + 1;
        }

        $conditions = array(
            "Producto.id" => $ids
        );
//debug($conditions);
        $db = $this->Producto->find('all', array('conditions' => $conditions));
//$db = $this->Producto->find('all');
        $completo = array();
        $i = 0;
//debug($db);
        foreach ($db as $producto) {
            $completo[$i] = $producto['Producto'];
//debug($completo[$i]);
            $i = $i + 1;
        }

        $this->set('children', $completo);
        $this->set('_serialize', array('children'));
    }

    public function afterFilter()
    {
        parent::afterFilter();

        if (empty($this->request->query['callback']) || $this->response->type() != 'application/json') {
            return;
        }

        // jsonp response
        App::uses('Sanitize', 'Utility');
        $callbackFuncName = Sanitize::clean($this->request->query['callback']);
        $out = $this->response->body();
        $out = sprintf("%s(%s)", $callbackFuncName, $out);
        $this->response->body($out);
    }


    public function beforeFilter()
    {
        $this->Auth->allow('getPlatosByCartaCategoria', 'getSeccionesDia');
        if ($this->RequestHandler->isAjax()) {
            Configure::write('debug', 0);
        }
    }
}
